package zhoufu.sz.sanxiao_check.ui.basic;

import android.databinding.ViewDataBinding;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import rx.Subscriber;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.databinding.ActivityBasicMainBinding;
import zhoufu.sz.sanxiao_check.event.Event;
import zhoufu.sz.sanxiao_check.event.RxBus;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.ui.check.CheckMainActivity;
import zhoufu.sz.sanxiao_check.utils.UIUtils;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/25
 */
public class BasicMainActivity extends BaseMsgActivity implements View.OnClickListener
{

    ActivityBasicMainBinding _binding;


    @Override protected void initInject()
    {
        setTitle(R.string.title_basic_message_in);
    }

    @Override protected int setIndividualId()
    {
        return R.layout.activity_basic_main;
    }

    @Nullable @Override protected android.databinding.DataBindingComponent setBindingComponent()
    {
        return LoginComponet.Instance.get();
    }

    @Override protected void initAfterOnCreate(ViewDataBinding binding)
    {
        super.initAfterOnCreate(binding);
        _binding = (ActivityBasicMainBinding) binding;
        _binding.setOnClickListener(this);
        _binding.setModel(mInfoModel);
        _binding.setToggle(zhoufu.sz.sanxiao_check.utils.TextUtils.isEmpty(mInfoModel.getSID()));
        rxBusInit();
    }

    private void rxBusInit()
    {
        RxBus.getDefault().toObserverable(Event.UpdateStreetInfo.class).subscribe(
                new Subscriber<Event.UpdateStreetInfo>()
                {
                    @Override public void onCompleted()
                    {

                    }

                    @Override public void onError(Throwable e)
                    {

                    }

                    @Override public void onNext(Event.UpdateStreetInfo updateStreetInfo)
                    {
                        mInfoModel = updateStreetInfo.getModel();
                        _binding.setModel(mInfoModel);
                    }
                });
    }

    @Override public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.mTvBasicItem1://责任登记卡
                _binding.expandableLayout.toggle();
                _binding.setToggle(_binding.expandableLayout.isExpanded());
                break;
            case R.id.mTvBasicMsg1:
                _navigator.startBasicMainAct(this, mInfoModel,
                        BasicMsgItem1Activity.class);
                break;
            case R.id.mTvBasicMsg2:
                if ( TextUtils.isEmpty(mInfoModel.getSID()) )
                {
                    UIUtils.showSnackbar(_binding.getRoot(), "请完善场所基本信息");
                    return;
                }
                _navigator.startBasicMainAct(this, mInfoModel,
                        BasicMsgItem2Activity.class);
                break;
            case R.id.mTvBasicItem2://责任告知书
                if ( TextUtils.isEmpty(mInfoModel.getSID()) )
                {
                    UIUtils.showSnackbar(_binding.getRoot(), "请完善场所基本信息");
                    return;
                }
                _navigator.startBasicMainAct(this, mInfoModel, BasicItem2Activity.class);
                break;
            case R.id.mTvBasicItem3://资料上传
                if ( TextUtils.isEmpty(mInfoModel.getSID()) )
                {
                    UIUtils.showSnackbar(_binding.getRoot(), "请完善场所基本信息");
                    return;
                }
                _navigator.startBasicMainAct(this, mInfoModel, BasicItem3Activity.class);
                break;
            case R.id.mTvBasicItem4://分级评定表
                if ( TextUtils.isEmpty(mInfoModel.getSID()) )
                {
                    UIUtils.showSnackbar(_binding.getRoot(), "请完善场所基本信息");
                    return;
                }
                _navigator.startBasicMainAct(this, mInfoModel, NewBasicItem4Activity.class);
                break;
            case R.id.mTvBasicItem5://安全检查
                if ( TextUtils.isEmpty(mInfoModel.getSID()) )
                {
                    UIUtils.showSnackbar(_binding.getRoot(), "请完善场所基本信息");
                    return;
                }
                _navigator.startBasicMainAct(this, mInfoModel, CheckMainActivity.class);
                break;
        }
    }
}
